import axios from 'axios';
import { fromJS } from 'immutable';
import  { actionType } from './index';

const changeStateDetaildata = (data)=>{
      return {
          type:actionType.CHANGE_DETAILDATE,
          data:fromJS(data)
      }
}


export const getDetailData = (id)=>{
    return (dispath)=>{
        axios.get('/detail.json?id=' + id)
        .then( (response)=> {
            let flag = false;
            if(response.data.code === '200'){
                const detail_data  = response.data.data;
                for(let i=0;i<detail_data.length;i++){
                    if(detail_data[i].id == id){
                        flag = true;
                        dispath(changeStateDetaildata(detail_data[i]));
                        return false;
                    }
                }
            }
            if(!flag){
                dispath(changeStateDetaildata({}));
            }
        })
        .catch( (error)=> {
            console.log(error);
        });
    }
}